clc;
global B_N R f l sigma;
B_N = 2000, R = 250*8, l = 5, sigma = 5;


% res = [];
% for snr = -30:0.01:30;
%     res = [res' ExpectPdr(snr,32)]'; % 100 is the payload length and 32 is the header length
%     
% end
% plot (-30:0.01:30, res);


targetsnr = 0;
s_snr=0.000;
count = 0;
pdrtosnr = [];
SNR_TO_PDR =[];
for snr = 0:0.1:10
%     if ( SnrToPdr(realpow(10, snr/10), 126) - 0.8 < 0.005 && SnrToPdr(realpow(10, snr/10), 126) - 0.80>0)
%        targetsnr = snr
%        targetpdr = SnrToPdr(realpow(10, snr/10), 126)
%     end
    
%     if ( abs (SnrToPdr(realpow(10,snr/10), 126) - s_snr) < 0.00005)
%        pdrtosnr = [ pdrtosnr' snr]';
%        s_snr = s_snr + 0.1;
%        count = count + 1;
%     end
    SNR_TO_PDR = [SNR_TO_PDR  SnrToPdr(realpow(10,snr/10), 126)];
end
SnrToPdr(realpow (10, 5/10), 126)

% pdr = 0:0.001:1;
% plot (pdrToGamma(pdr, 132), pdr);

 snr = -4:0.01:15;
 plot (snr,SnrToPdr(realpow (10, snr/10), 126));
 xlabel('SNR','fontsize',16);ylabel('PDR','fontsize',16);title('SNR vs PDR','fontsize',16);
 
 for snr = 0:0.001:10
     if (abs (SnrToPdr(realpow (10, snr/10), 126) -0.9) <0.0001)
         SNR= snr
         PDR = SnrToPdr(realpow (10, snr/10), 126)
         break;
     end
 end


% save the pdr-snr value pairs into a text file
  fileId = fopen('data.txt', 'w');
  fprintf( fileId, '%2.6f, ', SNR_TO_PDR);
  fclose (fileId);
 %fprintf( fileId, '%2.6f, %2.6f, %2.6f, %2.6f, %2.6f, %2.6f, %2.6f, %2.6f, %2.6f, %2.6f, \n', pdrtosnr);
%% calculate the slope and draw the figure
%slope = [];
%for i=1:1:999
%    slope =[slope' (pdrtosnr(i+1)-pdrtosnr(i) )/0.001]';
%end

%plot (1:1:999, slope);

% snr=11.5610 correspondent to pdr=99%